using System;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using WindowsLive.Writer.Extensibility.BlogClient;
using System.Xml;

namespace test
{
	[WriterPlugin("77b17bc8-1092-4872-9490-dc79b416d9f8", "Table Sizer",
	              Description = "Allows you to define the width tables should be set to automatically when a post is published.",
	              PublisherUrl = "https://tablesizer.codeplex.com/",
	              HasEditableOptions = true
	              )]
	public class TableSizerPlugin : PublishNotificationHook
	{
		//Settings & Default Values
		public static readonly string TableWidthSetting = "TableWidth";
		public static readonly string TableWidthSettingDefault = "100%";


		public static readonly string FixColumnWidthsSetting = "RemoveColumnWidths";
		public static readonly bool FixColumnWidthsSettingDefault = true;


		public override bool OnPrePublish (IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
		{
			BlogPost post = publishingContext.PostInfo as BlogPost;
			if (post == null) {
				MessageBox.Show (dialogOwner, "Cannot cast the Post to BlogPost, so TableSizer will not work", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				base.OnPrePublish (dialogOwner, properties, publishingContext, publish);
			}


			XmlDocument postXml = new XmlDocument ();
			//We can modify the contents.


			try {
				//Load with a document node wrapper & dtd to make it valid xml
				postXml.LoadXml ("<!DOCTYPE topElementName [ <!ENTITY nbsp \"&#x00A0;\"> ]><post>" + post.Contents + "</post>");
			} catch (Exception ex) {
				MessageBox.Show (dialogOwner, "Cannot parse post as XML because " + ex.Message +"\nMake sure you have configured WLW to use XHTML for your blog!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				base.OnPrePublish (dialogOwner, properties, publishingContext, publish);
			}

			//find all tables in the xml doc
			foreach (XmlElement table in postXml.SelectNodes("//table")) {
				table.SetAttribute("width", this.TableWidth);

				//Fix widths by removing explicit widths if this option is on
				if(this.FixColumnWidths)
				{
					foreach(XmlElement td in table.SelectNodes("//td"))
					{
						if(td.HasAttribute("width"))
						{
							td.RemoveAttribute("width");
						}
					}
				}

			}

			//Use the innerxml of the wrapper we created
			post.Contents = postXml.DocumentElement.InnerXml;

			return base.OnPrePublish (dialogOwner, properties, publishingContext, publish);
		}


	
		public override void Initialize (IProperties pluginOptions)
		{
			//Default value
			if(!pluginOptions.Contains(TableWidthSetting)) pluginOptions.SetString(TableWidthSetting, TableWidthSettingDefault);

			base.Initialize (pluginOptions);
		}

		public override void EditOptions (IWin32Window dialogOwner)
		{
			TableSizerOptionsForm options = new TableSizerOptionsForm ();
			options.TableWidth  = this.TableWidth;
			options.FixColumnWidths = this.FixColumnWidths;


			//TODO: should do some validation that the TableWidth setting is a valid HTML size
			DialogResult result = options.ShowDialog (dialogOwner);
			if (result == DialogResult.OK)
			{
				this.Options.SetString(TableWidthSetting, options.TableWidth);
				this.Options.SetBoolean(FixColumnWidthsSetting, options.FixColumnWidths);
			}
		}






		//Settings

		protected string TableWidth
		{
			get
			{
				return this.Options.GetString(TableWidthSetting, TableWidthSettingDefault);
			}
		}


		protected bool FixColumnWidths
		{
			get
			{
				return this.Options.GetBoolean(FixColumnWidthsSetting, FixColumnWidthsSettingDefault);
			}
		}


	}
}

